package com.applet.mallappletbackstage.service.impl;

import com.applet.mallappletbackstage.entity.Collect;
import com.applet.mallappletbackstage.entity.ReleaseWork;
import com.applet.mallappletbackstage.mapper.CollectDao;
import com.applet.mallappletbackstage.mapper.ReleaseWorkMapper;
import com.applet.mallappletbackstage.service.CollectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * (Collect)表服务实现类
 *
 * @author makejava
 * @since 2020-08-17 10:06:07
 */
@Service("collectService")
public class CollectServiceImpl extends ServiceImpl<CollectDao, Collect> implements CollectService {

    @Autowired
    CollectDao collectDao;
    @Autowired
    ReleaseWorkMapper releaseWorkMapper;

    @Override
    public IPage<ReleaseWork> selectAll(Long current, String user, String typeOfWork) {
        Page<Collect> collectPage = new Page<>(current, 15);
        QueryWrapper<Collect> collectQueryWrapper = new QueryWrapper<>();
        collectQueryWrapper.eq("type_of_work", typeOfWork);
        collectQueryWrapper.eq("user", user);
        List<Collect> records = collectDao.selectPage(collectPage, collectQueryWrapper).getRecords();
        if (records.isEmpty()) {
            return null;
        }
        List<String> collect = records.stream().map(Collect::getWork).collect(Collectors.toList());
        return getWork(current, typeOfWork, collect);
    }

    public IPage<ReleaseWork> getWork(Long current, String typeOfWork, List<String> work) {
        Page<ReleaseWork> page = new Page<>(current, 15);
        QueryWrapper<ReleaseWork> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type_of_work", typeOfWork);
        queryWrapper.in("id", work);
        return releaseWorkMapper.selectPage(page, queryWrapper);
    }
}
